package com.zzw.springcloud.controller;

import com.zzw.springcloud.bean.Member;
import com.zzw.springcloud.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

//http://localhost:80/member/consumer/selectById?id=2
@Slf4j
@Controller
@RequestMapping("/member/nacos/consumer")
public class MemberNacosConsumerController {
    //MEMBER_SERVICE_NACOS_PROVIDER_URL就是服务提供方注册到nacos server的名字
    public static final String MEMBER_SERVICE_NACOS_PROVIDER_URL = "http://member-service-nacos-provider";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/selectById/{id}")
    @ResponseBody
    public Result selectById(@PathVariable("id") Integer id) {
        //当你使用带@RibbonClient注解的restTemplate的时候，请求会被ribbon拦截，会将服务名替换成对应的ip端口
        Result result = restTemplate.getForObject(MEMBER_SERVICE_NACOS_PROVIDER_URL
                + "/member/nacos/provider/selectById/" + id, Result.class);
        return result;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody Member member) {
        Result result = restTemplate.postForObject(MEMBER_SERVICE_NACOS_PROVIDER_URL
                + "/member/nacos/provider/insert", member, Result.class);
        return result;
    }
}
